package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.app.AlertDialog;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import pub.devrel.easypermissions.AfterPermissionGranted;
import pub.devrel.easypermissions.EasyPermissions;

public class MainActivity extends AppCompatActivity {
    public static final int RESULT_CODE=1001;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //动态请求权限
        methodRequiresTwoPermission();

    }

    @AfterPermissionGranted(RESULT_CODE)
    private void methodRequiresTwoPermission() {
        String[] perms = {Manifest.permission.INTERNET, Manifest.permission.ACCESS_FINE_LOCATION};
        if (EasyPermissions.hasPermissions(this, perms)) {
            // 已有权限
            addLocationLis();
        } else {
            EasyPermissions.requestPermissions(this, "请求获取您的位置信息",
                    RESULT_CODE, perms);
        }
    }

    private void addLocationLis(){
        Button button = (Button) findViewById(R.id.button);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取位置服务
                LocationManager locationManager = (LocationManager) MainActivity.this.getSystemService(Context.LOCATION_SERVICE);
                Criteria criteria = new Criteria();
                //ACCURACY_HIGH/ACCURACY_LOW精度选择
                criteria.setAccuracy(Criteria.ACCURACY_FINE);
                //高度
                criteria.setAltitudeRequired(true);
                //方位信息
                criteria.setBearingRequired(true);
                //是否允许付费
                criteria.setCostAllowed(true);
                //对电量的要求
                criteria.setPowerRequirement(Criteria.POWER_LOW);
                //速度
                criteria.setSpeedRequired(true);
                //获取最佳服务
                String provider = locationManager.getBestProvider(criteria, true);
                //权限检查
                if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                    return;
                }
                //获取到了位置
                Location location = locationManager.getLastKnownLocation(provider);
                //开启地理位置监听定位类型、毫秒、米、监听时间
                locationManager.requestLocationUpdates(provider,3000,1,new listener());
                locateResult(location);

            }
        });
    }




    public class listener implements LocationListener {

        @Override
        public void onLocationChanged(Location location) {
            //位置变化，获取最新的位置
            locateResult(location);
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {

        }

        @Override
        public void onProviderEnabled(String provider) {

        }

        @Override
        public void onProviderDisabled(String provider) {

        }
    }

    public void locateResult(Location location){
        
        new AlertDialog.Builder(MainActivity.this)
                .setMessage("纬度" + String.valueOf(location.getLatitude()) +
                        "\n经度" + String.valueOf(location.getLongitude()) +
                        "\n速度" + String.valueOf(location.getSpeed()) +
                        "\n高度" + String.valueOf(location.getAltitude()) +
                        "\n朝向" + String.valueOf(location.getBearing())
                )
                .setNegativeButton("取消",null)
                .show();
    }


}